<?php

class AuthController extends Zend_Controller_Action
{
	private $Form;
	
	public function init()
	{
		$this->view->title = 'login';
		$this->initView();
		$this->view->baseUrl =  $this->_request->getBaseUrl();
		$this->view->UserError = '';
		$this->view->message   ='';
		$this->_helper->layout->disableLayout();
	}
	
	public function indexAction()
	{
		$form = new Form_Login();
		$this->view->loginform = $form;
		if ($this->_request->isPost()) 
		{
			$formData = $this->_request->getPost();
			
            if ($form->isValid($formData)) 
			{
				$authAdapter = new Cms_Auth_Adapter_Doctrine();
				$authAdapter->setTableName('AdminUser');
				$authAdapter->setIdentityColumn('username');
				$authAdapter->setCredentialColumn('password');
				$authAdapter->setIdentity($formData['UserName']);
				$authAdapter->setCredential(md5($formData['Password']));
				
				$auth = Zend_Auth::getInstance();
				$result = $auth->authenticate($authAdapter);
		
				if($result->isValid())
				{
				
					$result->getIdentity() === $auth->getIdentity();
					$result->getIdentity() === $formData['UserName'];
					$user = new Zend_Session_Namespace('MWC_Admin_Auth');
					
					$user->userId = $authAdapter->getResultRowObject()->user_id;
					$userId = $user->userId;
					
					echo "<pre>";
					print_r($_SESSION);
					
				} else {
						$this->view->UserError = "Username or Password incorrect";
				}
              
			
			  $form->populate($formData);
			  $this->render('index');
		}
	}
}	
	public function logoutAction()
	{
           Zend_Auth::getInstance()->clearIdentity();
		   print_r($_SESSION);
		   exit;
           header('location: /admin/index.php');
           // $this->_redirect('index');
	}
	
	public function forgotPasswordAction()
  	{
		$form    =    new Form_ForgotPassword;
		
		$this->view->form = $form;
			
		if ($this->_request->isPost()) 
	    {
			$formData = $this->_request->getPost();
			
			if($form->isValid($formData)) 
			{
			
				$query = new Doctrine_Query();
				$query->select('email,name,id')
				      ->from('Members')
					  ->where('email = ?',$_POST['Email']);
				$result = $query->execute();
				
				if($result->count())
				{
					$password = new GeneratePassword(10);
					
					$this->view->newpassword = $password->Password;
					$this->view->username    = $result[0]->name;
					
					 $query = Doctrine_Query::create();
					$query->update('Members')
					  	  ->set('password', '?',md5($password->Password))
						   ->where('id = '.$result[0]->id)
					  	  ->execute();
					
					$forgotPassword = new ForgotPassword();
					$forgotPassword->name = $result[0]->name;
					$forgotPassword->password = md5($password->Password);
					$forgotPassword->member_id = $result[0]->id;
					$forgotPassword->date = new Doctrine_Expression('NOW()');
					$forgotPassword->save();
					
					$mail = new Zend_Mail();
					$mail->setBodyHtml($this->view->render('forgot_password_mail.html'));
					$mail->setFrom('password@resumesindia.com');
					$mail->addTo($formData['Email'], 'Password Resminder');
					$mail->setSubject('New Password');
					$mail->send();	
					
					$this->view->message = 'Your account details has been sent to your Email Address';
				} 
				else
				{
					$this->view->message = 'This email address is not existing';
				} 
			}
		}     
	}
}
?>